import { TodoType } from "./store";
export type ActionType =
  | { type: "add"; payload: TodoType }
  | { type: "remove"; payload: string }
  | { type: "update"; payload: TodoType };

export type dispatchType<T> = (state: T, action: ActionType) => T;
const reducer: dispatchType<TodoType[]> = (state: TodoType[], action: ActionType) => {
  switch (action.type) {
    case "add":
      return [...state, action.payload];
    case "update":
      return state.map((item) => {
        if (item.id === action.payload.id) {
          return action.payload;
        }
        return item;
      });
    case "remove":
      return state.filter((item) => item.id !== action.payload);
    default:
      return state;
  }
};
export default reducer;
